package com.doudizu.shoesonline.controller;

import com.doudizu.shoesonline.common.BaseResponse;
import com.doudizu.shoesonline.common.ErrorCode;
import com.doudizu.shoesonline.common.ResultUtils;
import com.doudizu.shoesonline.exception.BusinessException;
import com.doudizu.shoesonline.model.dto.user.UserDTO;
import com.doudizu.shoesonline.model.dto.user.UserQuery;
import com.doudizu.shoesonline.model.entity.User;
import com.doudizu.shoesonline.model.vo.UserVO;
import com.doudizu.shoesonline.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.doudizu.shoesonline.constant.UserConstant.USER_LOGIN;

/**
 * @author ln
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {

    @Resource
    private UserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/current")
    @ApiOperation("获取当前登录用户")
    public BaseResponse getCurrentUser(String token) {
        if (token == null) {
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "未登录");
        }
        String userId = redisTemplate.opsForValue().get(USER_LOGIN + token);
        UserVO safeUser = userService.getUser(Integer.valueOf(userId));
        return ResultUtils.success(safeUser);
    }

    @PostMapping("/sendEmailCode")
    @ApiOperation("发送邮箱验证码")
    public BaseResponse sendEmailCode(String email) {
            userService.sendEmailCode(email);
            return ResultUtils.success(null);
    }

    @PostMapping("/delete")
    @ApiOperation("删除用户")
    public BaseResponse deleteUser(@RequestParam("ids") List<Integer> ids) {
        userService.removeBatchByIds(ids);
        return ResultUtils.success(null);
    }

    @PostMapping("/list")
    @ApiOperation("用户列表")
    public BaseResponse getUsers(UserQuery userQuery) {
        return ResultUtils.success(userService.getUsers(userQuery));
    }

    @GetMapping("/search")
    @ApiOperation("查询用户")
    public BaseResponse searchUsers(String gender, String userName) {
        return ResultUtils.success(userService.searchUsers(gender, userName));
    }


    @PostMapping("/lockOrUnlock")
    @ApiOperation(value = "单个或批量修改状态", notes = "只传id和修改后的status")
    public BaseResponse lockOrUnlock(@RequestBody List<User> userList) {
        userService.updateBatchById(userList);
        return ResultUtils.success(null);
    }


    @GetMapping
    @ApiOperation("获取单个用户信息")
    public BaseResponse getUser(Integer userId) {
        return ResultUtils.success(userService.getUser(userId));
    }


    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    public BaseResponse updateUser(@RequestBody UserDTO user) {
        return ResultUtils.success(userService.updateUser(user));
    }


    @PostMapping("/register")
    @ApiOperation("注册")
    public BaseResponse<Long> userRegister(@RequestBody UserDTO user) {
        if (user == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入正确的信息");
        }
        userService.userRegister(user);
        return ResultUtils.success(null);
    }

    @PostMapping("/add")
    @ApiOperation("添加用户")
    public BaseResponse addUser(@RequestBody UserDTO user) {
        if (user == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入正确的信息");
        }
        userService.addUser(user);
        return ResultUtils.success(null);
    }


    @PostMapping("/login")
    @ApiOperation("登录")
    public BaseResponse<Map> userLogin(String account, String password) {
        if (StringUtils.isAnyBlank(account, password)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String token = userService.userLogin(account, password);
        String userId = redisTemplate.opsForValue().get(USER_LOGIN + token);
        User user = userService.getById(userId);
        Map<String,Object> res = new HashMap<>();
        res.put("token",token);
        res.put("user",user);
        return ResultUtils.success(res);
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public BaseResponse<Boolean> userLogout(String token) {
        if (token == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.userLogout(token);
        return ResultUtils.success(result);
    }

    @PostMapping("reset")
    @ApiOperation("忘记密码")
    public BaseResponse reset(String password, String account, String answer) {
        return ResultUtils.success(userService.reset(password, account, answer));
    }
}
